package com.bbzn.device.client.controller;

import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.OssConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.service.OssService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.service.impl.CommonServiceImpl;
import com.bbzn.device.client.utils.BaseApiService;
import com.bbzn.device.client.utils.BaseResponse;
import com.bbzn.device.client.utils.MayiktJwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author: 曾俞钧
 * @Date : 2021/3/24 14:37
 * @Version 1.0
 */
@RestController
@Api(value = "OSS", tags = "OSS云存储")
@RequestMapping("/ossUser")
//@CrossOrigin
public class OssController extends BaseApiService {

    @Autowired
    OssService ossService;
    @Autowired
    private SystemParamByCacheService systemParamByCacheService;
    @Autowired
    CommonServiceImpl commonService;

    @ApiOperation(value = "上传头像", produces = "multiple/form-data")
    @RequestMapping(value = "/addOssHederImg", method = RequestMethod.POST)
    public BaseResponse ossHederImg(MultipartFile file, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        SystemParam systemParam = null;
        if (Objects.isNull(file)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_NU_ERROR_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        //头像文件大小不超过10MB
        if (file.getSize() > 10 * 1024 * 1024) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_SIZE_10MB_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        String imgUrl = ossService.uploadHeadImg(file);
        if (Objects.isNull(imgUrl)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_FAILED_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_SUCCESS_PREFIX + language);
        return setResult(1, systemParam.getMassage(), imgUrl);
    }


    /**
     * 删除头像 图片
     *
     * @param imgUrl\ * @return
     * @throws Exception
     */
    @ApiOperation(value = "删除头像")
    @RequestMapping(value = "/delOssHederImg", method = RequestMethod.POST)
    public BaseResponse delOssHederImg(String imgUrl, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        SystemParam systemParam = null;
        if (Objects.isNull(imgUrl)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_NU_ERROR_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        List<String> list = new ArrayList<>();
        list.add(imgUrl.substring(46));
        List list2 = ossService.delListImg(list);
        if (Objects.isNull(list2)) {
            systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_FAIL_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 上传视频
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "上传视频", produces = "multiple/form-data")
    @RequestMapping(value = "/uploadVideo", method = RequestMethod.POST)
    public BaseResponse uploadVideo(MultipartFile file, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        SystemParam systemParam = null;

        if (Objects.isNull(file)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_NU_ERROR_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        String videoUrl = ossService.uploadHeadImg(file);
        if (Objects.isNull(videoUrl)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_FAILED_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_SUCCESS_PREFIX + language);
        return setResult(1, systemParam.getMassage(), videoUrl);
    }

    /**
     * 上传视频
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "上传excel", produces = "multiple/form-data")
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    public BaseResponse uploadExcel(MultipartFile file, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        Long userId = commonService.getUserId(request);
        SystemParam systemParam = null;
        if (Objects.isNull(file)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_NU_ERROR_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        String fileUrl = ossService.uploadExcel(file);
        if (Objects.isNull(fileUrl)) {
            systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_FAILED_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        systemParam = systemParamByCacheService.getValues(OssConstant.OSS_ENUM, OssConstant.OSS_SUCCESS_PREFIX + language);
        return setResult(1, systemParam.getMassage(), fileUrl);
    }


}
